require 'pyro'
require 'thor'

# Namespaces Pyro::CLI.
module Pyro
  # Public: Command line interface for Pyro.
  #
  # Example
  #
  #   ~ $ pyro create MyApp
  class CLI < Thor
    include Thor::Actions

    # Sets the root directory for source files.
    def self.source_root
      "#{File.dirname(__FILE__)}/../.."
    end

    desc 'burn', 'Compiles the app for production'
    long_desc <<-LONGDESC
    Builds and minifies the app for production and, if set, use the prod_src and prod_dir helper arguments in index.erb.

    Use this command to before deploying, or to test a production build in conjunction with the `pyro serve -e production` command.

    Example: pyro burn
    LONGDESC
    # Public: Builds and minifies the app for production.
    #
    # Example
    #
    #   ~/MyApp $ pyro burn
    #
    # Writes a minified production build to /public by calling Pyro.burn().
    def burn
      Pyro.burn
    end

    desc 'create [APP]', 'Creates a new app'
    long_desc <<-LONGDESC
    Creates a new Pyro app. The [APP] argument should be the name of your app.

    Example: pyro create MyApp
    LONGDESC
    # Public: Creates a new Pyro app.
    #
    # app - The name or path of the new app.
    #
    # Example
    #
    #   ~ $ pyro create MyApp
    #
    # Copies /templates/pyro into /MyApp.
    def create(app)
      directory 'templates/pyro', app
    end

    desc 'serve', 'Runs the app on localhost:7976'
    option :environment, aliases: :e, default: 'development'
    long_desc <<-LONGDESC
    Runs the app on localhost:7976. Use -e to set the Rack environment. `development` (default) will rebuild the app on each page reload. `test` will rebuild the app on each page reload and run the test_* helpers. `production` will not rebuild the app or run the test_* helpers.

    Example: pyro serve -e production
    LONGDESC
    # Public: Runs an app on localhost:7976.
    #
    # environment - The environment to run on, defaults to development.
    #
    # Example
    #
    #   ~/MyApp $ pyro serve test
    #
    # Runs an app on a local server by calling Pyro.serve().
    def serve
      Pyro.serve options[:environment]
    end
  end
end